﻿using System;
using System.Collections.Generic;
using System.Net;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Utility;
using System.Linq;
using ZFCNew.Controls;
using System.Windows;

namespace ZFCNew.Code
{
    /// <summary>
    /// Publish, Comments, etc
    /// </summary>
    public partial class FacebookHelper
    {
        public static event EventHandler<SearchBaseEventArgs<stream_data>> GetNewsFeedCompleted;

        public static void PostMessage(string message, string description, string caption)
        {

            var mediaAttachments = new List<attachment_media>();
            var previewImageAttachment = new attachment_media_image();
            previewImageAttachment.href = "http://mrgni.com";
            previewImageAttachment.src = "http://upload.wikimedia.org/wikipedia/en/9/9e/Zakumi.jpg";
            previewImageAttachment.type = attachment_media_type.image;
            mediaAttachments.Add(previewImageAttachment);


            var attachment = new attachment
                                        {
                                            name = "ZFC",
                                            description = description,
                                            caption = caption,
                                            media = mediaAttachments,
                                            href = "http://mrgni.com",
                                        };

            action_link al = new action_link
            {
                text = "View ZFC",
                href = "http://mrgni.com",


            };
            IList<action_link> als = new List<action_link> { al };
            _fb.Stream.PublishAsync(message, attachment, als, null, 0, OnStreamPublish, null);

        }

        public static void GetNewsFeed(int maxNumber)
        {
            _fb.Stream.GetAsync(null, DateTime.Now - TimeSpan.FromDays(30), null, maxNumber, OnGetStream, null);

        }
        static void OnGetStream(stream_data data, Object state, FacebookException e)
        {
            if (e == null)
            {
                if (GetNewsFeedCompleted != null)
                {
                    GetNewsFeedCompleted(null, new SearchBaseEventArgs<stream_data> { Results = data });
                }
            }

        }












        static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

        }

        private static void OnStreamPublish(string id, Object state, FacebookException e)
        {
            if (e == null)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                              {
                                                                  MainPage.NotificationPop.SetMessage
                                                                      (
                                                                          new NotificationData
                                                                              {
                                                                                  Message =
                                                                                      "Your message is successfully posted in your wall."
                                                                              }

                                                                      );
            });
                MainPage.NotificationPop.Show();
            }
            else
            {
                 Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                               {
                                                                   MainPage.NotificationPop.SetMessage
                                                                       (
                                                                           new NotificationData
                                                                               {

                                                                                   Message =
                                                                                       "Oops! We could not post your message to facebook ... Try again later."
                                                                               }
                                                                       );
                                                               });
                MainPage.NotificationPop.Show();
            }
        }



    }
}
